<?php

defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
set_html('title',$rooms['share_title']?:'新秒杀活动');
$setting=$this->plugin_setting('basic');
if($_RQ['op']=='main'){
	$taskid = intval($_RQ['taskid']);
		if (empty($taskid)) {
			$taskid = $this->model('model')->getTodaySeckill();
			if (empty($taskid)) {
				//exi('今日没有秒杀，看看别的商品吧~','error','jy_weishop/index/');	
			}
		}
		$task =  $this->model('model')->getTaskInfo($taskid);
		if (empty($task)) {
			//exi('未找到秒杀任务','error');
		}
		$rooms =  $this->model('model')->getRooms($taskid);
		if (empty($rooms)) {
			//exi('未找到秒杀会场','error');
		}
		$room = false;
		$roomindex = 0;
		$roomid = intval($_RQ['roomid']);
		//获取当前会场
		if (empty($roomid)) {
			foreach ($rooms as $row) {
				$room = $row;
				break;
			}
		}else {
			foreach ($rooms as $index => $row) {
				if ($row['id'] == $roomid) {
					$room = $row;
					$roomindex = $index;
					break;
				}
			}
		}
		if (empty($room)) {
			//exi('未找到秒杀会场','error');
		}
		$roomid = $room['id'];
		$timeid = 0;
		$currenttime = time();
		$timeindex = -1;
		$return['taskid']=$taskid;
		//获取专题时间点
		$alltimes =  $this->model('model')->getTaskTimes($taskid);
		//$alltimes=pdo_getcolumn('jy_seckill_task',array('id'=>$taskid,'uniacid'=>$_SESSION['uniacid']),'times');
		//$alltimes=str_array($alltimes);
		$times = array();
		$validtimes = array();
		paixu($alltimes,'time','asc');
		//print_r($alltimes);exit;
		foreach ($alltimes as $key => $tv) {
			$time['time']=$tv['time'];
			if (strlen($time['time']) == 1) {
				$timestr = '0' . $time['time'];
			}else{
				$timestr=$time['time'];
			}
			$starttime = strtotime(date('Y-m-d ' . $timestr . ':00:00'));
			$oldshow = true;
			$timegoods =  $this->model('model')->getSeckillGoods($taskid, $time['time'], 'all');
			$hasGoods = false;
			if($timegoods){
				foreach ($timegoods as $tg) {
					//if ($tg['timeid'] == $roomid) {
						$hasGoods = true;
						break;
					//}
				}
			}
			if (isset($alltimes[$key + 1])) {
				$end = $alltimes[$key + 1]['time'] - 1;
				$time['end']=$end;
				$endtime = strtotime(date('Y-m-d ' . $end . ':59:59'));
			}else{
				$endtime = strtotime(date('Y-m-d 23:59:59'));
			}
			if($setting['maxhours']>=1){
				$settime=$setting['maxhours']*3600+$starttime;
				$endtime=$settime<$endtime?$settime:$endtime;
			}
			if ($endtime < $currenttime) {
				if (!$room['oldshow']) {
					$oldshow = false;
				}
			}
			if ($hasGoods && $oldshow) {
				$validtimes[] = $time;
			}
		}
		$return['alltimes']=$alltimes;
		$return['validtimes']=$validtimes;
		foreach ($validtimes as $key => $time) {
			$timestr = $time['time'];
			if (strlen($timestr) == 1) {
				$timestr = '0' . $timestr;
			}
			$starttime = strtotime(date('Y-m-d ' . $timestr . ':00:00'));
			if (isset($validtimes[$key + 1])) {
				$end = $validtimes[$key + 1]['time'] - 1;
				$endtime = strtotime(date('Y-m-d ' . $end . ':59:59'));
			}else {
				$endtime = strtotime(date('Y-m-d 23:59:59'));
			}
			if($setting['maxhours']>=1){
				$settime=$setting['maxhours']*3600+$starttime;
				$endtime=$settime<$endtime?$settime:$endtime;
			}
			$time['endtime'] = $endtime;
			$time['starttime'] = $starttime;
			$time['endtime2'] = date('Y-m-d H',$endtime);
			$time['starttime2'] = date('Y-m-d H',$starttime);
			$time['etime'] = $endtime-time();
			$time['stime'] = $starttime-time();
			$time['time'] = $timestr;
			if (($starttime <= $currenttime) && ($currenttime <= $endtime)) {
				$time['status'] = 1;
				$timeid = $time['time'];
				if ($timeindex == -1) {
					$timeindex = $key;
				}
				$time['rest_time']=model('jy_weishop/model')->date($endtime-TIMESTAMP);
				$time['rest_time2']=$endtime-TIMESTAMP;		
				$times[] = $time;
			}else if ($currenttime < $starttime) {
				$time['status'] = 0;
				if (empty($timeid)) {
					$timeid = $time['time'];
				}
				$time['rest_time']=model('jy_weishop/model')->date($starttime-TIMESTAMP);
				$time['rest_time2']=$starttime-TIMESTAMP;
				$times[] = $time;
			}else {
				if ($endtime < $currenttime) {
					$time['status'] = -1;
					if (empty($timeid)) {
						$timeid = $time['time'];
					}
				}
			}
			
	}
	if ($timeindex == -1) {
			$timeindex = 0;
	}
	$return['cartnums']= $_SESSION['uid']?pdo_getsum('jy_weishop_member_cart',array('mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total'):0;
	$count = count($times); 
	if($count==0){
		$roomid=0;
		$rooms=array();
	}
	$sharelink='jy_seckill/index//fromuser='.$_SESSION['uid'];
	$return['sharedata']=array(
			'title'=>$rooms['share_title']?$rooms['share_title']:'新秒杀活动,享大折扣！',
			'desc'=>$rooms['share_desc']?$rooms['share_desc']:'新秒杀活动，更能享受好的购物折扣哦！',
			'link'=>custom_url($sharelink,true),
			'imgUrl'=>$rooms['share_image']?tomedia($rooms['share_image']):tomedia('../addons/jy_weishop/resource/images/sales/luck_share_image.jpg')
	);
	$return['times']=$times;
	$return['rooms']=$rooms;
	$return['roomid']=$roomid;
	$return['timeindex']=$timeindex;
	$return['timeid']=$timeid;
	$return['currenttime']=$currenttime;
	$return['advs']=pdo_getall('jy_seckill_adv',array('uniacid'=>$_SESSION['uniacid'],'enabled'=>1));
	$return['current_plugin']=$_SESSION['current_plugin']=='jy_seckill'?'jy_weishop':$_SESSION['current_plugin'];	
	exi($return);
}